package com.wxy.customview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.Scroller
import com.wxy.customview.ext.dp

/**
 * desc   :
 * author : xiangyuan.wang
 * date   : 2020-07-31
 * 大小 ：固定大小
 * 绘制 ：每个大刻度宽度为6  小刻度宽度为4  刻度间距4
 *
 *
 *
 *
 */
class BooheeScale @JvmOverloads constructor(context: Context, attributes: AttributeSet? = null, defStyleAttr: Int = 0)
    : View(context, attributes, defStyleAttr) {
    private val TAG: String = "BooheeRule"
    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private val max: Int = 2000
    private val mScaleWidth = 2.dp
    private val mScaleHeight = 15.dp
    private val mLargeScaleWidth = 6.dp
    private val mLargeScaleHeight = 30.dp
    private val mSpace = 10.dp
    private val mTextSpace = 8.dp

    val scalePaint: Paint = Paint().apply {
        color = Color.GRAY
        isAntiAlias = true
        strokeWidth = mScaleWidth.toFloat()
        strokeCap = Paint.Cap.ROUND
    }
    val largeScalePaint = Paint().apply {
        color = Color.GREEN
        isAntiAlias = true
        strokeWidth = mLargeScaleWidth.toFloat()
        strokeCap = Paint.Cap.ROUND
    }

    val textPaint = Paint().apply {
        color = Color.WHITE
        isAntiAlias = true
        textSize = 14.dp.toFloat()
        textAlign = Paint.Align.CENTER
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        mWidth = TuvUtils.getDefaultSize(widthMeasureSpec, (max - 1) * mSpace)
        mHeight = TuvUtils.getDefaultSize(heightMeasureSpec, (paddingTop + mLargeScaleHeight
                + mTextSpace + textPaint.fontMetrics.bottom - textPaint.fontMetrics.top + paddingBottom).toInt())
        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        for (i: Int in 0 until max) {
            val centerStartX = i / 10 * (10 * mScaleWidth + 9 * mSpace + mLargeScaleWidth) + mLargeScaleWidth / 2
            val remainder = i % 10
            if (remainder == 0) {
                canvas?.drawLine(centerStartX.toFloat(),
                        (mHeight - mLargeScaleHeight).toFloat(),
                        centerStartX.toFloat(),
                        mHeight.toFloat(),
                        largeScalePaint)
                canvas?.drawText("$i", centerStartX.toFloat(), -textPaint.fontMetrics.top, textPaint)
            } else {
                val x = (centerStartX + remainder * mSpace + (remainder - 1) * mScaleWidth).toFloat()
                canvas?.drawLine(x,
                        (mHeight - mScaleHeight).toFloat(),
                        x,
                        mHeight.toFloat(),
                        scalePaint)
            }
        }
    }

}